<template>
  <div class="login-container">
    <div class="register-box">
      <div class="login-box">
        <h3 style="font-size: 35px">已有帐号?</h3>
        <p style="margin-top: 50px">请登录 🚀</p>
        <a-button class="register-button" @click="goToLogin"> 登录 </a-button>
      </div>
      <div class="register-form">
        <h2>注册</h2>
        <a-form
          ref="formRef"
          :rules="rules"
          :model="registerForm"
          @finish="handleFinish"
        >
          <div>
            <a-form-item name="username">
              <a-input
                v-model:value="registerForm.username"
                placeholder="用户名"
              />
            </a-form-item>

            <a-form-item has-feedback name="password">
              <a-input-password
                v-model:value="registerForm.password"
                autocomplete="off"
                placeholder="密码"
              />
            </a-form-item>
            <a-form-item has-feedback name="rePassword">
              <a-input-password
                v-model:value="registerForm.rePassword"
                autocomplete="off"
                placeholder="重复密码"
              />
            </a-form-item>
            <a-form-item name="email">
              <a-input v-model:value="registerForm.email" placeholder="邮箱" />
            </a-form-item>
            <a-form-item name="phone">
              <a-input v-model:value="registerForm.phone" placeholder="电话" />
            </a-form-item>
          </div>
          <div class="form-footer">
            <a-button type="primary" html-type="submit" class="login-button">
              注册
            </a-button>
          </div>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { Rule } from "ant-design-vue/es/form";
import type { FormInstance } from "ant-design-vue";
import { registerApi } from "@/api/user";
import { message } from "ant-design-vue";

const router = useRouter();
const formRef = ref<FormInstance>();
const registerForm = reactive({
  username: "",
  password: "",
  rePassword: "",
  email: "",
  phone: "",
});

const validatePasswd = async (_rule: Rule, value: string) => {
  if (value === "") {
    return Promise.reject("请输入密码");
  } else {
    if (registerForm.password !== "") {
      if (formRef.value) {
        formRef.value.validateFields("checkPass");
      }
    }
    return Promise.resolve();
  }
};
// 自定义重复密码校验
const validateRePasswd = async (_rule: Rule, value: string) => {
  if (value === "") {
    return Promise.reject("请重复输入密码");
  } else if (value !== registerForm.password) {
    return Promise.reject("两次密码输入不一致");
  } else {
    return Promise.resolve();
  }
};

const rules: Record<string, Rule[]> = {
  username: [{ required: true, message: "请输入用户名" }],
  password: [{ required: true, validator: validatePasswd, trigger: "change" }],
  rePassword: [
    { required: true, validator: validateRePasswd, trigger: "change" },
  ],
  email: [{ required: true, type: "email", message: "请输入正确邮箱格式" }],
  phone: [{ required: true, len: 11, message: "请输入手机号" }],
};

const handleFinish = async () => {
  //处理注册的逻辑
  let result = await registerApi(registerForm);
  console.log("==================");
  console.log(result.data);
  if (result.data.code === 200) {
    //成功了
    message.success("注册成功! 即将跳转到登录界面...");
    //延迟1s后返回登录界面
    setTimeout(() => {
      router.push("/login");
    }, 1000);
  } else {
    //失败了
    alert("注册失败");
  }
};

const forgotPassword = () => {
  // 处理忘记密码逻辑
};

const goToLogin = () => {
  router.push("/login");
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url("@/assets/images/screen.png");
  background-size: cover;
  /* background: linear-gradient(135deg, #f5f7fa 0%, #c2c9d4 100%); */
  padding: 20px;
}

.register-box {
  display: flex;
  border-radius: 15px;
  overflow: hidden;
  width: 800px;
  max-height: 700px; /* 增加最小高度 */
  backdrop-filter: blur(5px);
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.register-form {
  flex: 1;
  padding: 50px 50px 30px 50px;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(5px);
}

.login-box {
  flex: 1;
  /* background: rgba(255, 77, 79, 0.8); */
  background: linear-gradient(
    135deg,
    rgba(242, 110, 112, 0.9) 0%,
    rgba(255, 19, 101, 0.9) 100%
  );
  backdrop-filter: blur(7px);
  color: white;
  padding: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

h2 {
  margin-bottom: 30px;
  /* margin-left: 110px; */
  color: #333;
  font-size: 35px;
  text-align: center;
}

.form-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 24px;
}

:where(.css-dev-only-do-not-override-1p3hq3p).ant-input-affix-wrapper {
  padding: 0px 11px;
}

.forgot-password {
  color: #666;
  transition: color 0.3s;
}

.forgot-password:hover {
  color: #ff4d4f;
}

.login-button {
  background: #ff4d4f;
  border-color: #ff4d4f;
  width: 120px;
  height: 40px;
  font-size: 16px;
}

.login-button:hover {
  background: #ff7875;
  border-color: #ff7875;
}

.register-button {
  background: transparent;
  border: 2px solid white;
  color: white;
  margin-top: 20px;
  height: 40px;
  width: 140px;
  font-size: 16px;
  transition: all 0.3s;
}

.register-button:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: white;
  color: white;
}

.register-box h3 {
  color: white;
  margin-bottom: 12px;
  font-size: 24px;
}

.register-box p {
  color: white;
  margin-bottom: 24px;
  font-size: 16px;
}

:deep(.ant-input),
:deep(.ant-input-password) {
  height: 40px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.9);
}

:deep(.ant-input-password .ant-input) {
  height: 38px;
  background: transparent;
}

:deep(.ant-input-password-icon) {
  color: rgba(0, 0, 0, 0.45);
  margin-top: -2px;
}

:deep(.ant-form-item) {
  margin-bottom: 24px;
}

@media (max-width: 768px) {
  .login-box {
    flex-direction: column;
    width: 100%;
    max-width: 500px;
  }

  .login-form,
  .register-box {
    width: 100%;
    padding: 30px;
  }
}
</style>
